package org.bambrikii.dictionary.dao;

import java.util.List;

import org.bambrikii.dictionary.Article;
import org.bambrikii.site.base.core.dao.DAOBase;
import org.bambrikii.site.base.persistence.PersistenceConnector;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class ArticleDAO extends DAOBase<Article> implements IArticleDAO {
	public ArticleDAO(final PersistenceConnector connector) {
		super(connector);
	}

	@SuppressWarnings("unchecked")
	public List<Article> getArticles(final String title) {
		final Criteria crit = getCriteria();
		crit.add(Restrictions.like("title", title));
		crit.addOrder(Order.desc("insertionTime"));
		return crit.list();
	}

	private Criteria getDefaultCriteria(final String title) {
		final Criteria crit = getCriteria();
		crit.add(Restrictions.like("title", title));
		return crit;
	}

	public Integer getCount(final String title) {
		final Criteria crit = getDefaultCriteria(title);
		crit.setProjection(Projections.rowCount());
		return (Integer) crit.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Article> getList(final String title, final String order,
			final Integer start, final Integer length) {
		final Criteria crit = getDefaultCriteria(title);
		setOrder(crit, order);
		return crit.list();
	}
}
